import json
import random
from datetime import datetime, timedelta

def generate_user(user_id):
    return {
        "id": user_id,
        "name": f"User {user_id}",
        "email": f"user{user_id}@example.com",
        "address": {
            "street": f"{random.randint(100, 999)} Main St",
            "city": random.choice(["Anytown", "Othertown", "Sometown"]),
            "state": random.choice(["CA", "NY", "TX"]),
            "postalCode": f"{random.randint(10000, 99999)}",
            "coordinates": {
                "latitude": round(random.uniform(-90, 90), 6),
                "longitude": round(random.uniform(-180, 180), 6),
                "randomValue": round(random.random(), 9)
            }
        },
        "orders": [generate_order(i) for i in range(random.randint(5, 20))],
        "preferences": {
            "theme": random.choice(["dark", "light"]),
            "notifications": {
                "email": random.choice([True, False]),
                "sms": random.choice([True, False]),
                "push": random.choice([True, False])
            },
            "language": random.choice(["English", "Spanish", "French"])
        }
    }

def generate_order(order_id):
    return {
        "orderId": f"ORD{order_id}",
        "products": [generate_product(i) for i in range(random.randint(1, 10))],
        "totalAmount": round(random.uniform(50, 1000), 2),
        "orderDate": (datetime.now() - timedelta(days=random.randint(1, 365))).isoformat() + "Z"
    }

def generate_product(product_id):
    return {
        "productId": f"P{product_id}",
        "name": random.choice(["Laptop", "Mouse", "Keyboard", "Monitor", "Headphones"]),
        "price": round(random.uniform(10, 1000), 2),
        "quantity": random.randint(1, 5),
        "details": {
            "brand": random.choice(["BrandX", "BrandY", "BrandZ"]),
            "specs": {
                "cpu": random.choice(["Intel i5", "Intel i7", "AMD Ryzen 5"]),
                "ram": random.choice(["8GB", "16GB", "32GB"]),
                "storage": random.choice(["256GB SSD", "512GB SSD", "1TB HDD"])
            }
        }
    }

def generate_large_json(num_users):
    users = [generate_user(i) for i in range(1, num_users + 1)]
    return {
        "users": users,
        "metadata": {
            "totalUsers": len(users),
            "totalOrders": sum(len(user["orders"]) for user in users),
            "generatedAt": datetime.now().isoformat() + "Z"
        }
    }

# 生成 1000 个用户的 JSON 数据
data = generate_large_json(2000)

# 保存为 JSON 文件
with open("large_data.json", "w") as f:
    json.dump(data, f, indent=2)

print("Generated large JSON file: large_data.json")